<template>
   
  <div class="app-container">
    <el-card style="margin-top: 15px" class="operate-container" shadow="never">
      <i class="el-icon-tickets"></i>
      <span>数据列表</span>
      <el-button
        class="btn-add"
        style="float: right"
        @click="addDialogVisible = true"
        size="mini"
      >
        添加
      </el-button>

      <el-dialog
        title="地址添加"
        :visible.sync="addDialogVisible"
        width="60%"
        :before-close="handleClose"
      >
        <span>
          <el-card class="filter-container" shadow="never">
            <div style="margin-top: 15px">
              <el-form
                :rules="rules"
                ref="addAddress"
                :inline="true"
                :model="addAddress"
                size="small"
                label-width="140px"
              >
                <el-form-item label="省份:">
                  <el-select
                    v-model="searchNetworkList.provinceId"
                    style="width: 203px"
                    placeholder="请选择要搜索的省份"
                    @change="showCity"
                    clearable
                  >
                    <el-option
                      v-for="(item, index) in provinceOptions"
                      :key="index"
                      :label="item.name"
                      :value="item.id"
                    >
                    </el-option>
                  </el-select>
                </el-form-item>

                <el-form-item label="城市:">
                  <el-select
                    v-model="searchNetworkList.cityId"
                    style="width: 203px"
                    placeholder="请选择要搜索的城市"
                    @change="showArea"
                    clearable
                  >
                    <el-option
                      v-for="(item, index) in cityOptions"
                      :key="index"
                      :label="item.name"
                      :value="item.id"
                    >
                    </el-option>
                  </el-select>
                </el-form-item>

                <el-form-item label="地区:">
                  <el-select
                    v-model="searchNetworkList.areaId"
                    style="width: 203px"
                    placeholder="请选择要搜索的地区"
                    @change="getNetworkList"
                    clearable
                  >
                    <el-option
                      v-for="(item, index) in areaOptions"
                      :key="index"
                      :label="item.name"
                      :value="item.id"
                    >
                    </el-option>
                  </el-select>
                </el-form-item>

                <el-form-item label="网点名称:">
                  <el-select
                    v-model="addAddress.networkName"
                    clearable
                    filterable
                    style="width: 203px"
                    placeholder="请输入要搜索的网点名称"
                  >
                    <el-option
                      v-for="(item, index) in NetworkOptions"
                      :key="index"
                      :label="item.name"
                      :value="item.id"
                    >
                    </el-option>
                  </el-select>
                </el-form-item>

                <el-form-item prop="name" label="姓名:">
                  <el-input
                    style="width: 203px"
                    v-model="addAddress.name"
                    placeholder="请输入姓名"
                    clearable
                  ></el-input>
                </el-form-item>

                <el-form-item label="手机号:">
                  <el-input
                    style="width: 203px"
                    v-model="addAddress.phone"
                    placeholder="请输入手机号"
                    clearable
                  ></el-input>
                </el-form-item>

                <el-form-item prop="addres" label="详细地址:">
                  <el-input
                    style="width: 203px"
                    v-model="addAddress.address"
                    placeholder="请输入详细地址"
                    clearable
                  ></el-input>
                </el-form-item>

                <el-form-item prop="email" label="邮箱:">
                  <el-input
                    style="width: 203px"
                    v-model="addAddress.email"
                    placeholder="请输入邮箱"
                    clearable
                  ></el-input>
                </el-form-item>
              </el-form>
            </div>
          </el-card>
        </span>
        <span slot="footer" class="dialog-footer">
          <el-button @click="addDialogVisible = false">取 消</el-button>
          <el-button type="primary" @click="handleAdd()">确 定</el-button>
        </span>
      </el-dialog>
    </el-card>
    <div style="margin-top: 15px" class="table-container">
      <el-table
        :data="AddressList"
        style="width: 100%"
        height="260px"
        v-loading="listLoading"
        border
      >
        <el-table-column
          type="selection"
          width="60"
          align="center"
        ></el-table-column>

        <el-table-column label="地址ID" width="150" align="center">
          <template slot-scope="scope">{{ scope.row.id }}</template>
        </el-table-column>
        <el-table-column label="网点名称" width="150" align="center">
          <template slot-scope="scope">{{ scope.row.network.name }}</template>
          <!--networkId -->
        </el-table-column>
        <el-table-column label="姓名" width="200" align="center">
          <template slot-scope="scope">{{ scope.row.name }}</template>
        </el-table-column>
        <el-table-column label="手机号" width="200" align="center">
          <template slot-scope="scope">{{
            scope.row.phone
          }}</template> </el-table-column
        >x
        <el-table-column label="详细地址" width="200" align="center">
          <template slot-scope="scope">{{ scope.row.address }}</template>
        </el-table-column>
        <el-table-column label="邮箱" width="200" align="center">
          <template slot-scope="scope">{{ scope.row.email }}</template>
        </el-table-column>
        <el-table-column fixed="right" label="操作" width="200" align="center">
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="primary"
              @click="
                (messageEditDialogVisible = true), showMessageData(scope.$index)
              "
              >信息编辑
            </el-button>
            <el-button
              size="mini"
              type="danger"
              @click="handledelete(scope.row.id)"
              >删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-dialog
        title="地址信息编辑"
        :visible.sync="messageEditDialogVisible"
        width="60%"
        :before-close="handleClose"
      >
        <span>
          <el-card class="filter-container" shadow="never">
            <div style="margin-top: 15px">
              <el-form
                :rules="editMessageRules"
                ref="editMessage"
                :inline="true"
                :model="editMessage"
                size="small"
                label-width="140px"
              >
                <el-form-item prop="id" label="地址ID:">
                  <el-input
                    :disabled="true"
                    style="width: 203px"
                    v-model="editMessage.id"
                    placeholder="请输入地址ID"
                    clearable
                  ></el-input>
                </el-form-item>

                <el-form-item label="网点名称:">
                  <el-select
                    v-model="editMessage.networkName"
                    clearable
                    filterable
                    style="width: 203px"
                    placeholder="请输入要搜索的网点名称"
                    @change="$forceUpdate()"
                  >
                    <el-option
                      v-for="(item, index) in NetworkOptions"
                      :key="index"
                      :label="item.name"
                      :value="item.id"
                    >
                    </el-option>
                  </el-select>
                </el-form-item>

                <el-form-item label="姓名:">
                  <el-input
                    style="width: 203px"
                    v-model="editMessage.name"
                    placeholder="请输入姓名"
                    clearable
                  ></el-input>
                </el-form-item>

                <el-form-item label="手机号:">
                  <el-input
                    style="width: 203px"
                    v-model="editMessage.phone"
                    placeholder="请输入手机号"
                    clearable
                  ></el-input>
                </el-form-item>

                <el-form-item label="详细地址:">
                  <el-input
                    style="width: 203px"
                    v-model="editMessage.address"
                    placeholder="请输入详细地址"
                    clearable
                  ></el-input>
                </el-form-item>

                <el-form-item label="邮箱:">
                  <el-input
                    style="width: 203px"
                    v-model="editMessage.email"
                    placeholder="请输入邮箱"
                    clearable
                  ></el-input>
                </el-form-item>
              </el-form>
            </div>
          </el-card>
        </span>
        <span slot="footer" class="dialog-footer">
          <el-button @click="messageEditDialogVisible = false">取 消</el-button>
          <el-button type="primary" @click="handleEditMessage()"
            >确 定</el-button
          >
        </span>
      </el-dialog>
    </div>
    <div class="block">
      <el-pagination
        @current-change="handleCurrentChange"
        layout="prev, pager, next"
        :page-count="pageCunt"
        :current-page.sync="currentPage"
      >
      </el-pagination>
    </div>
  </div>
</template>
  <script>
export default {
  name: "brancheManage",
  data() {
    return {
      provinceOptions: [],
      cityOptions: [],
      areaOptions: [],
      searchNetworkList: {
        pageNum: "1",
        pageSize: "5",
        provinceId: "",
        cityId: "",
        areaId: "",
      },
      currentPage: 1,
      NetworkOptions: [],
      currentAddressList: {
        pageNum: "1",
        pageSize: "5",
      },
      AddressList: [],
      addAddress: {
        networkName: "",
        networkId: "",
        name: "",
        phone: "",
        address: "",
        email: "",
      },
      editMessage: {
        id: "",
        name: "",
        phone: "",
        networkId: "", //networkId
        networkName: "",
        address: "",
        email: "",
        desc: "",
      },
      listLoading: false,
      addDialogVisible: false,
      messageEditDialogVisible: false,
      pageCunt: 1,
      statusEditDialogVisible: false,
      rules: {
        address: [
          { required: true, message: "请输入详细地址", trigger: "blur" },
        ],
        name: [{ required: true, message: "请输入姓名", trigger: "blur" }],
        networkId: [{ required: true, message: "网点名称", trigger: "blur" }],
        id: [{ required: true, message: "请输入地址ID", trigger: "blur" }],
        Phone: [{ required: true, message: "请输入手机号", trigger: "blur" }],
        email: [{ required: true, message: "请输入邮箱", trigger: "blur" }],
      },
      editMessageRules: {
        address: [
          { required: true, message: "请输入详细地址", trigger: "blur" },
        ],
        name: [{ required: true, message: "请输入姓名", trigger: "blur" }],
        networkId: [
          { required: true, message: "请输入网点ID", trigger: "blur" },
        ],
        id: [{ required: true, message: "请输入地址ID", trigger: "blur" }],
        Phone: [{ required: true, message: "请输入手机号", trigger: "blur" }],
        email: [{ required: true, message: "请输入邮箱", trigger: "blur" }],
      },
    };
  },
  created() {
    this.getList();
    this.getProvince();
  },
  methods: {
    getProvince() {
      this.axios
        .get("api/sys/province/list")
        .then((response) => {
          this.provinceOptions = response.data.value;
        })
        .catch((error) => {});
    },
    showCity(item) {
      this.searchNetworkList.cityId = "";
      this.searchNetworkList.areaId = "";
      this.cityOptions = [];
      this.areaOptions = [];
      if (item != 0) {
        this.axios
          .get("api/sys/city/list?provinceId=" + item)
          .then((response) => {
            this.cityOptions = response.data.value;
            this.getNetworkList();
          })
          .catch((error) => {});
      }
    },
    showArea(item) {
      this.searchNetworkList.areaId = "";
      this.areaOptions = [];
      if (item != 0) {
        this.axios
          .get("api/sys/area/list?cityId=" + item)
          .then((response) => {
            this.areaOptions = response.data.value;
            this.getNetworkList();
          })
          .catch((error) => {});
      }
    },
    getList() {
      this.listLoading = true;
      this.axios
        .get("api/auth/customer/address/list", {
          params: this.currentAddressList,
        })
        .then((response) => {
          console.log(response);
          if (response.data.code == "200") {
            this.AddressList = response.data.value.data;
            if (
              response.data.value.total / 5 >
              Number.parseInt(response.data.value.total / 5)
            ) {
              this.pageCunt =
                Number.parseInt(response.data.value.total / 5) + 1;
            } else {
              this.pageCunt = Number.parseInt(response.data.value.total / 5);
            }
            this.currentPage = 1;
          } else {
            this.$message.error(response.data.message);
          }
          this.listLoading = false;
        })
        .catch((error) => {
          this.$message.error("无法获取数据");
          this.listLoading = false;
        });
    },
    handleClose(done) {
      this.$confirm("确认关闭？")
        .then((_) => {
          done();
        })
        .catch((_) => {});
    },
    handleCurrentChange(val) {
      this.listLoading = true;
      this.currentAddressList.pageNum = val;
      this.axios
        .get("api/auth/customer/address/list", {
          params: this.currentAddressList,
        })
        .then((response) => {
          if (response.data.code == "200") {
            this.AddressList = response.data.value.data;
          } else {
            this.$message.error(response.data.message);
          }
          this.listLoading = false;
        })
        .catch((error) => {
          this.$message.error("无法获取数据");
          this.listLoading = false;
        });
    },
    handleAdd() {
      this.$refs.addAddress.validate((valid) => {
        if (valid) {
          this.$confirm("请再次确认是否添加")
            .then((_) => {
              this.axios
                .post(
                  "api/auth/customer/address/add?address=" +
                    this.addAddress.address +
                    "&name=" +
                    this.addAddress.name +
                    "&networkId=" +
                    this.addAddress.networkName +
                    "&phone=" +
                    this.addAddress.phone +
                    "&email=" +
                    this.addAddress.email
                )
                .then((response) => {
                  console.log(response);
                  if (response.data.code == "200") {
                    this.$message({
                      message: "添加成功",
                      type: "success",
                    });
                    this.getList();
                  } else {
                    this.$message.error(response.data.message);
                  }

                  this.addDialogVisible = false;
                })
                .catch((error) => {
                  this.$message.error("添加失败");
                });
            })
            .catch((_) => {});
        } else {
          this.$message.error("请填写完将带*号的选项");
        }
      });
    },
    showMessageData(index) {
      this.editMessage = { ...this.AddressList[index] };
      this.editMessage.networkName = this.AddressList[index].network.id;
    },
    handleEditMessage() {
      this.$refs.editMessage.validate((valid) => {
        if (valid) {
          this.$confirm("请再次确认是否选择修改")
            .then((_) => {
              this.axios
                .post(
                  "api/auth/customer/address/update?address=" +
                    this.editMessage.address +
                    "&id=" +
                    this.editMessage.id +
                    "&name=" +
                    this.editMessage.name +
                    "&networkId=" +
                    this.editMessage.networkName +
                    "&phone=" +
                    this.editMessage.phone +
                    "&email=" +
                    this.editMessage.email
                )
                .then((response) => {
                  console.log(response);
                  if (response.data.code == "200") {
                    this.$message({
                      message: "修改成功",
                      type: "success",
                    });
                    this.getList();
                    this.messageEditDialogVisible = false;
                  } else {
                    this.$message.error(response.data.message);
                  }
                })
                .catch((error) => {
                  this.$message.error("修改失败");
                });
            })
            .catch((_) => {});
        } else {
          this.$message.error("请填写完将带*号的选项");
        }
      });
    },
    showStatusData(index) {
      this.editStatus = { ...this.AddressList[index] };
    },
    handledelete(id) {
      this.$confirm("请再次确认是否删除")
        .then((_) => {
          this.axios
            .get("api/auth/customer/address/delete?id=" + id)
            .then((response) => {
              console.log(response);
              if (response.data.code == "200") {
                this.$message({
                  message: "删除成功",
                  type: "success",
                });
                this.getList();
              } else {
                this.$message.error(response.data.message);
              }
            })
            .catch((error) => {
              this.$message.error("删除失败");
            });
        })
        .catch((_) => {});
    },
    getNetworkList() {
      this.listLoading = true;
      this.axios
        .get("api/auth/network/list", {
          params: this.searchNetworkList,
        })
        .then((response) => {
          if (response.data.code == "200") {
            this.NetworkOptions = response.data.value.data;
          } 
        })
        .catch((error) => {
          this.$message.error("无法获取数据");
          this.listLoading = false;
        });
    },
  },
};
</script>
  <style></style>
  
  
  